﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CSLADesignerLibrary3;



namespace CSLADesigner
{
    

    public partial class CSLADesigner : Form
    {

        
        Dictionary<string,string> roBusinessBaseProperties = new Dictionary<string, string>();
        Dictionary<string, string> bbProperties = new Dictionary<string, string>();

        string fname;
        string ftype;


        #region TabControl Methods

        private void HideAllTabs()
        {
            HideTabPage(BusinessMethods);
            HideTabPage(DataAccess);
            HideTabPage(Exists);
            HideTabPage(FactoryMethods);
            HideTabPage(AuthorizationRules);
            HideTabPage(ValidationRules);
            HideTabPage(Constructors);
            HideTabPage(ReadOnlyListDataAccessTab);
            HideTabPage(ReadOnlyBusinessMethods);
        }
        
        private void HideTabPage(TabPage tp)
        {
            if (tabControl1.TabPages.Contains(tp))
                tabControl1.TabPages.Remove(tp);
        }


        private void ShowTabPage(TabPage tp)
        {
            ShowTabPage(tp, tabControl1.TabPages.Count);
        }


        private void ShowTabPage(TabPage tp, int index)
        {
            if (tabControl1.TabPages.Contains(tp)) return;
            InsertTabPage(tp, index);
        }


        private void InsertTabPage(TabPage tabpage, int index)
        {
            if (index < 0 || index > tabControl1.TabCount)
                throw new ArgumentException("Index out of Range.");
            tabControl1.TabPages.Add(tabpage);
            if (index < tabControl1.TabCount - 1)
                do
                {
                    SwapTabPages(tabpage, (tabControl1.TabPages[tabControl1.TabPages.IndexOf(tabpage) - 1]));
                }
                while (tabControl1.TabPages.IndexOf(tabpage) != index);
            tabControl1.SelectedTab = tabpage;
        }


        private void SwapTabPages(TabPage tp1, TabPage tp2)
        {
            if (tabControl1.TabPages.Contains(tp1) == false || tabControl1.TabPages.Contains(tp2) == false)
                throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");

            int Index1 = tabControl1.TabPages.IndexOf(tp1);
            int Index2 = tabControl1.TabPages.IndexOf(tp2);
            tabControl1.TabPages[Index1] = tp2;
            tabControl1.TabPages[Index2] = tp1;

            //Uncomment the following section to overcome bugs in the Compact Framework
            //tabControl1.SelectedIndex = tabControl1.SelectedIndex; 
            //string tp1Text, tp2Text;
            //tp1Text = tp1.Text;
            //tp2Text = tp2.Text;
            //tp1.Text=tp2Text;
            //tp2.Text=tp1Text;

        }
        #endregion

       



        public CSLADesigner()
        {
            InitializeComponent();
        }

        

        

        public void ValidateForm()
        {
            if (txtNamespace.Text == string.Empty)
            {
               
                MessageBox.Show("Namespace cannot be blank");
                
                
            }

            if (txtObjectName.Text == string.Empty)
            {

                MessageBox.Show("Object name cannot be blank");


            }
                
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            //initialize CSLADesigner Here
            CSLADesignerLib3 GenLib = new CSLADesignerLib3();
            
            ValidateForm();
            StringBuilder thecode = new StringBuilder();
            if (rdoCsharp.Checked)
            {
                //Generate CSharp Code
                thecode.AppendLine(GenLib.MakeHeaderCS(lstboxInheritsFrom.SelectedItem.ToString()));
                thecode.AppendLine(GenLib.MakeNamespaceStartCS(txtNamespace.Text));
                thecode.AppendLine(GenLib.ClassDeclarationCS(lstboxInheritsFrom.SelectedItem.ToString(),txtObjectName.Text));
            }
            else
            {
                //Generate VB Code
                thecode.Append(GenLib.ClassDeclarationVB(lstboxInheritsFrom.SelectedItem.ToString(),txtObjectName.Text));
            }
            if (lstboxInheritsFrom.SelectedItem.ToString() == "BusinessBase")
            {
                if (rdoCsharp.Checked)
                {
                    //Generate CSharp Code
                    thecode.AppendLine(GenLib.MakeBusinessMethodsCS(bbProperties));
                    thecode.AppendLine(GenLib.MakeBusinessBaseValidationRulesCS());
                    thecode.AppendLine(GenLib.MakeBusinessBaseAuthorizationRulesCS());
                    thecode.AppendLine(GenLib.MakeBusinessBaseFactoryMethodsCS(txtObjectName.Text));
                    thecode.AppendLine(GenLib.MakeBusinessBaseDataAccessCS(bbProperties,txtDataConnectionName.Text,txtBBDataAccessGetSingleStoredProc.Text,txtBBAddObjectStoredProc.Text, txtBBUpdateStoredProc.Text, txtDeleteObjectStoredProc.Text));
                    thecode.AppendLine(GenLib.MakeBusinessBaseBExistsCS(txtObjectName.Text,txtDataConnectionName.Text,txtObjectExistsStoredProc.Text));
                }
                else
                {
                    //Generate VB Code
                    thecode.AppendLine(GenLib.MakeBusinessMethodsVB(bbProperties));
                    thecode.AppendLine(GenLib.MakeValidationRulesVB());
                    thecode.AppendLine(GenLib.MakeAuthorizationRulesVB());
                    thecode.AppendLine(GenLib.MakeBBFactoryMethodsVB(txtObjectName.Text));
                    thecode.AppendLine(GenLib.MakeBBDataAccessVB(bbProperties, txtDataConnectionName.Text, txtBBDataAccessGetSingleStoredProc.Text, txtBBAddObjectStoredProc.Text, txtBBUpdateStoredProc.Text, txtDeleteObjectStoredProc.Text));
                    thecode.AppendLine(GenLib.MakeBBExistsVB(txtObjectName.Text, txtDataConnectionName.Text, txtObjectExistsStoredProc.Text));

                }
            }

            if (lstboxInheritsFrom.SelectedItem.ToString() == "ReadOnlyBase")
            {
                if (rdoCsharp.Checked)
                {
                    thecode.AppendLine(GenLib.MakeReadOnlyBusinessBaseCS(roBusinessBaseProperties));
                    thecode.AppendLine(GenLib.MakeReadOnlyConstructorsCS(txtObjectName.Text, roBusinessBaseProperties));
                }
                else
                {
                    thecode.AppendLine(GenLib.MakeReadOnlyBusinessBaseVB(roBusinessBaseProperties));
                    thecode.AppendLine(GenLib.MakeReadOnlyConstructorsVB(txtObjectName.Text, roBusinessBaseProperties));
                }
            }

            if (lstboxInheritsFrom.SelectedItem.ToString() == "ReadOnlyListBase")
            {
                if (rdoCsharp.Checked)
                {
                    thecode.AppendLine(GenLib.MakeReadOnlyListBaseFactoryMethodsCS(txtObjectName.Text,lstBoxFilteredCriteriaType.SelectedItem.ToString(),txtFilteredCriteriaName.Text));
                    thecode.AppendLine(GenLib.MakeReadOnlyListBaseDataAccessCS(txtObjectName.Text, lstBoxFilteredCriteriaType.SelectedIndex,lstBoxFilteredCriteriaType.SelectedItem.ToString(),txtFilteredCriteriaName.Text, txtDataConnectionName.Text, txtROLBSP.Text));
                }
                else
                {
                    thecode.AppendLine(GenLib.MakeReadOnlyListBaseFactoryMethodsVB(txtObjectName.Text, lstBoxFilteredCriteriaType.SelectedItem.ToString(), txtFilteredCriteriaName.Text));
                    thecode.AppendLine(GenLib.MakeReadOnlyListBaseDataAccessVB(txtObjectName.Text, lstBoxFilteredCriteriaType.SelectedIndex, lstBoxFilteredCriteriaType.SelectedItem.ToString(), txtFilteredCriteriaName.Text, txtDataConnectionName.Text, txtROLBSP.Text));
                }
            }
            if (rdoCsharp.Checked)
            {
                thecode.AppendLine(GenLib.MakeCloseClassCS());
            }
            else
            {
                //do vb
                thecode.AppendLine(GenLib.MakeCloseClassVB());
           
            }
            CodePreview cp = new CodePreview();
            cp.CodePreviewString = thecode.ToString();
            cp.Show();

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            HideAllTabs();
        }



        private void lstboxInheritsFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            HideAllTabs();
            if (lstboxInheritsFrom.SelectedItem.ToString() == "BusinessBase")
            {
                ShowTabPage(BusinessMethods);
                ShowTabPage(ValidationRules);
                ShowTabPage(AuthorizationRules);
                ShowTabPage(FactoryMethods);
                ShowTabPage(DataAccess);
                ShowTabPage(Exists);
            }

            if (lstboxInheritsFrom.SelectedItem.ToString() == "ReadOnlyBase")
            {
                ShowTabPage(ReadOnlyBusinessMethods);
                ShowTabPage(Constructors);
            }

            if (lstboxInheritsFrom.SelectedItem.ToString() == "ReadOnlyListBase")
            {
                ShowTabPage(FactoryMethods);
                ShowTabPage(ReadOnlyListDataAccessTab);
            }
         }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            //txtTMP.Text = string.Empty;
            //txtTMP.Update();
            try
            {
                roBusinessBaseProperties.Add(txtROBusinessName.Text, lstboxROBusinessTypes.SelectedItem.ToString());
            }
            catch (ArgumentException)
            {
            }
            StringBuilder teststring = new StringBuilder();
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                teststring.AppendFormat("{0}, {1}", kvp.Value.ToString(), kvp.Key.ToString());
                teststring.AppendLine();
                
            }

            txtTMP.Text = teststring.ToString();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtTMP.Text = string.Empty;
        }

        private void btnBusinessBaseAddProperty_Click(object sender, EventArgs e)
        {
            try
            {
                bbProperties.Add(txtBusinessBasePropertyName.Text, lstboxBusinessBaseProperties.SelectedItem.ToString());
            }
            catch (ArgumentException)
            {
            }
            StringBuilder bbProptmp = new StringBuilder();
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                bbProptmp.AppendFormat("{0}, {1}", kvp.Value.ToString(), kvp.Key.ToString());
                bbProptmp.AppendLine();
            }
            txtBBProperties.Text = bbProptmp.ToString();
        }
        
    }

       
    
}

